package gopay

import (
	"chess_platform/internal/gopay/client"
	"chess_platform/internal/gopay/common"
	"chess_platform/internal/gopay/constant"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"
	"github.com/astaxie/beego"
)

var (
	//支付地址
	//TODO:这两个要去除了
	payServerIp   = ""
	PayServerPort = ""

	//微信支付参数
	WeChatAppID          = "wx8f830b9bd5c342eb"
	WeChatMchID          = "1451572202"
	WeChatKey            = "S7Xsi466Em1HcF3scyCk0qlD09Jbxvd1"
	WeChatPayURL         = "https://api.mch.weixin.qq.com/pay/unifiedorder"
	WeChatQueryURL       = "https://api.mch.weixin.qq.com/pay/orderquery"
	WeChatWebCallBackURL = fmt.Sprintf("http://%v:%v/v1/h5/weChatWebPay-callback", payServerIp, PayServerPort)

	//支付宝支付参数
	AliProductCode    = "QUICK_MSECURITY_PAY" //售产品码，商家和支付宝签约的产品码
	AliAppID          = "2017101709355902"    // 支付宝分配给开发者的应用ID ps: 查询订单用
	AliPartnerID      = "2088411557088890"    // 支付宝合作身份ID
	AliSellerID       = "xxxxx"               // 卖家支付宝用户号
	AliPayURL         = "https://mapi.alipay.com/gateway.do"
	AliAppCallBackURL = fmt.Sprintf("http://%v:%v/v1/h5/aliWebPay-callback", payServerIp, PayServerPort)
	AliReturnURL      = ""
	AliPublicKEY      = []byte(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgevjINJXbsub6HYv
aRKKMrsT5UUD2TbpbV6oIiALMPBZZ5qPKfLuseDTt9uc5mSq5aTPNSnVZruW
TYbFIrx6NejVtprUd/oU152boR1Eb9gAF/eSj0UnH0G4EkdIEzo/URHC+Ggm
hjA67ONl+yfBCJqKjqfJvGyp47e7qfcX9FNrNmehBK1e3c9nvpkaeLNtg3KL
3+RaK2wTEw3bQVdV0z1BWJh2ZkI77Um//VIU7hZAo+w8cOtAxFKeToh3z0Og
OTY2tuW0fG2KlJrwZtifTUsAqgBrX/2JUe7zRUrg8WPkS8XW/0jQ/5pAMsyQ
tvA+czLx1KH/Un4tA/2YdBr+AwIDAQAB
-----END PUBLIC KEY-----`)

	publicKEY = []byte(`-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3g4caLzqNHSLaLW8u
SjRn3JuH+ch2mKrs1pPgCWfJiJ5hLdPJEdfKVKMHF+kNnSuzKc3VFWVsm7IDQ
z4PvkSpDZ/FY+oMvXb4Jaah44gvl7KFELn+4J4nRUlp4P0nzaNCMpolzMicZF
zirIxeqzAFtIasvr8T+NI3hN5bWWxGt35v93zOeW+NGmhX+GSH9K/Isxwfs0s
FYWswyax6jJF8HF9FyPy5bb9RkLhHyuDQdljJtsmOIbZkmYKNTyH8OXjgCLSW
ktfRvw2pxjZ61WoiLSpz55FHba+bdfcNIf4xI+vakA7CWze2DPkyL9b7q1hND
+3ZwIUei8nkwliRZLvewIDAQAB
-----END PUBLIC KEY-----`)

	privateKEY = []byte(`-----BEGIN RSA PRIVATE KEY-----
MIIEwAIBADANBgkqhkiG9w0BAQEFAASCBKowggSmAgEAAoIBAQDeDhxovOo0dI
totby5KNGfcm4f5yHaYquzWk+AJZ8mInmEt08kR18pUowcX6Q2dK7MpzdUVZWy
bsgNDPg++RKkNn8Vj6gy9dvglpqHjiC+XsoUQuf7gnidFSWng/SfNo0IymiXMy
JxkXOKsjF6rMAW0hqy+vxP40jeE3ltZbEa3fm/3fM55b40aaFf4ZIf0r8izHB+
zSwVhazDJrHqMkXwcX0XI/Lltv1GQuEfK4NB2WMm2yY4htmSZgo1PIfw5eOAIt
JaS19G/DanGNnrVaiItKnPnkUdtr5t19w0h/jEj69qQDsJbN7YM+TIv1vurWE0
P7dnAhR6LyeTCWJFku97AgMBAAECggEBAIcRnkY3Y1605Xez1C/snFWYjVPqi7
UC/94AuQsB12Si47fOqN0wbLn99CvuITzejNT/gbBSvtPift0xXm8voh8w5ux+
jZkx46Ug9q4tzcz+a30Xl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wj0DLIZgAXpnXmrHIyt0OU2978VR
OWUjCiy/UWsh+q4=
-----END RSA PRIVATE KEY-----`)
)

//初始化支付相关
func InitPayInfo() {
	client.InitWxWebClient(&client.WechatWebClient{
		AppID:       WeChatAppID,
		MchID:       WeChatMchID,
		Key:         WeChatKey,
		CallbackURL: WeChatWebCallBackURL,
		PayURL:      WeChatPayURL,
	})
	//私钥
	block, _ := pem.Decode(privateKEY)
	if block == nil {
		panic("签名密钥key decode失败")
	}
	PrivateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
	if err != nil {
		panic(err)
	}

	//公钥
	block, _ = pem.Decode(publicKEY)
	if block == nil {
		panic("签名公钥key decode失败")
	}
	PublicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		panic(err)
	}

	client.InitAliWebClient(&client.AliWebClient{
		PartnerID:   AliPartnerID,
		SellerID:    AliSellerID,
		AppID:       AliAppID,
		PrivateKey:  PrivateKey.(*rsa.PrivateKey),
		PublicKey:   PublicKey.(*rsa.PublicKey),
		PayURL:      AliPayURL,
		CallbackURL: AliAppCallBackURL,
	})

}

//微信Web支付
//TODO:要注意金额，小数点
func WeChatWebPay(moneyFee int64, describe, orderId string) (map[string]string, error) {
	charge := &common.Charge{
		PayMethod: constant.WECHAT_WEB,
		MoneyFee:  float64(moneyFee / 100), //交易金额
		Describe:  describe,                //商品描述
		TradeNum:  orderId,                 //交易单号
	}
	return Pay(charge)
}

//支付宝Web支付
func AliWebPay(moneyFee int64, describe, orderId string) (map[string]string, error) {
	charge := &common.Charge{
		PayMethod: constant.ALI_WEB,
		MoneyFee:  float64(moneyFee / 100), //交易金额
		Describe:  describe,                //商品描述
		TradeNum:  orderId,                 //交易单号
	}
	return Pay(charge)
}
